package com.yunquan.ohana.ui.calendar.sync

import androidx.databinding.ObservableField
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.ui.ItemViewModel
import com.yunquan.ohana.entity.FramesDataManager
import com.yunquan.ohana.entity.ScheduleModelVo
import com.yunquan.ohana.ui.calendar.event.dialog.EventSyncDialog
import com.yunquan.ohana.utils.Utils
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId

class SyncItemViewModel(viewModel: SyncViewModel) : ItemViewModel<SyncViewModel>(viewModel) {
    var data: ScheduleModelVo? = null
    val title = ObservableField<String>()
    val dateTime = ObservableField<String>()


    constructor(viewModel: SyncViewModel, data: ScheduleModelVo) : this(viewModel) {
        this.data = data
        title.set(data.title)

        if (data.allDay == 1) {
            dateTime.set(Utils.instance.getString(R.string.all_day))
        } else {
            val startDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(data.startTime!!), ZoneId.systemDefault())
            val endDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(data.endTime!!), ZoneId.systemDefault())
            dateTime.set("${startDateTime.toLocalDate()} ${startDateTime.toLocalTime()} - ${endDateTime.toLocalTime()}")
        }
    }

    val onSyncClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            if (data != null) {
                viewModel.toSync(data!!)
            }
        }
    })
}